package com.hrp.analyzer.model;

import com.hrp.analyzer.core.helper.AnalyzerHelper;
import com.hrp.analyzer.util.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;


/**
 * @author hrp
 * @date 2020/12/23 18:42
 */

public class Novel implements Serializable {
    /**
     * 小说所属站点
     */
    private String site;
    /**
     * 目录链接
     */
    private String url;
    /**
     * 书名
     */
    private String title;
    /**
     * 作者
     */
    private String author;
    /**
     * 播音
     */
    private String broadcast;
    /**
     * 分类
     */
    private String category;
    /**
     * 字数
     */
    private String wordCount;
    /**
     * 简介
     */
    private String introduce;
    /**
     * 最新章节名称
     */
    private String latestChapterName;
    /**
     * 最新章节链接
     */
    private String latestChapterUrl;
    /**
     * 封面
     */
    private String coverUrl;
    /**
     * 状态
     */
    private String state;
    /**
     * 最近更新时间
     */
    private String updateTime;
    /**
     * 章节列表
     */
    private transient List<Chapter> chapters;

    /**
     * 拼接URL到完整路径
     *
     * @param baseUrl 基准URL
     */
    public void competeUrl(String baseUrl) {
        AnalyzerHelper.completeUrl(baseUrl, this::getCoverUrl, this::setCoverUrl);
        AnalyzerHelper.completeUrl(baseUrl, this::getUrl, this::setUrl);
        AnalyzerHelper.completeUrl(baseUrl, this::getLatestChapterUrl, this::setLatestChapterUrl);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Novel novel = (Novel) o;
        return Objects.equals(url, novel.url);
    }

    @Override
    public int hashCode() {
        return Objects.hash(url);
    }

    /**
     * 去除首尾空白
     */
    public void trim() {
        if (title != null) {
            this.title = StringUtils.trim(title);
        }
        if (author != null) {
            this.author = StringUtils.trim(author);
        }
        if (broadcast != null) {
            this.broadcast = StringUtils.trim(broadcast);
        }
        if (category != null) {
            this.category = StringUtils.trim(category);
        }
        if (wordCount != null) {
            this.wordCount = StringUtils.trim(wordCount);
        }
        if (introduce != null) {
            this.introduce = StringUtils.trim(introduce);
        }
        if (latestChapterName != null) {
            this.latestChapterName = StringUtils.trim(latestChapterName);
        }
        if (state != null) {
            this.state = StringUtils.trim(state);
        }
        if (updateTime != null) {
            this.updateTime = StringUtils.trim(updateTime);
        }
    }

    public String getSite() {
        return site;
    }

    public void setSite(String site) {
        this.site = site;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getBroadcast() {
        return broadcast;
    }

    public void setBroadcast(String broadcast) {
        this.broadcast = broadcast;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getWordCount() {
        return wordCount;
    }

    public void setWordCount(String wordCount) {
        this.wordCount = wordCount;
    }

    public String getIntroduce() {
        return introduce;
    }

    public void setIntroduce(String introduce) {
        this.introduce = introduce;
    }

    public String getLatestChapterName() {
        return latestChapterName;
    }

    public void setLatestChapterName(String latestChapterName) {
        this.latestChapterName = latestChapterName;
    }

    public String getLatestChapterUrl() {
        return latestChapterUrl;
    }

    public void setLatestChapterUrl(String latestChapterUrl) {
        this.latestChapterUrl = latestChapterUrl;
    }

    public String getCoverUrl() {
        return coverUrl;
    }

    public void setCoverUrl(String coverUrl) {
        this.coverUrl = coverUrl;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public List<Chapter> getChapters() {
        return chapters;
    }

    public void setChapters(List<Chapter> chapters) {
        this.chapters = chapters;
    }

    @Override
    public String toString() {
        return "Novel{" +
                "site='" + site + '\'' +
                ", url='" + url + '\'' +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", broadcast='" + broadcast + '\'' +
                ", category='" + category + '\'' +
                ", wordCount='" + wordCount + '\'' +
                ", introduce='" + introduce + '\'' +
                ", latestChapterName='" + latestChapterName + '\'' +
                ", latestChapterUrl='" + latestChapterUrl + '\'' +
                ", coverUrl='" + coverUrl + '\'' +
                ", state='" + state + '\'' +
                ", updateTime='" + updateTime + '\'' +
                ", chapters=" + chapters +
                '}';
    }
}
